package org.jeecg.modules.clue.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.clue.entity.ClueRecordTask;
import org.jeecg.modules.clue.entity.ClueTaskQueryLog;
import org.jeecg.modules.clue.enums.*;
import org.jeecg.modules.clue.factory.carPlate.CarPlateService;
import org.jeecg.modules.clue.mapper.ClueTaskQueryLogMapper;
import org.jeecg.modules.clue.service.IClueRecordTaskService;
import org.jeecg.modules.clue.service.IClueTaskQueryLogService;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Description: 任务查询日志
 * @Author: jeecg-boot
 * @Date: 2025-01-10
 * @Version: V1.0
 */
@Service
@Slf4j
public class ClueTaskQueryLogServiceImpl extends ServiceImpl<ClueTaskQueryLogMapper, ClueTaskQueryLog> implements IClueTaskQueryLogService {


    @Resource(name = "TaskQueryThreadPool")
    ThreadPoolTaskExecutor taskQueryThreadPool;
    @Resource
    Map<String, CarPlateService> carPlateServiceMap;
    @Resource
    IClueRecordTaskService clueRecordTaskService;


    /**
     * 查询线索
     *
     * @param batchNo
     */
    @Override
    public void queryClueBatchNo(String batchNo) {
        List<ClueTaskQueryLog> logList = list(new LambdaQueryWrapper<ClueTaskQueryLog>().eq(ClueTaskQueryLog::getBatchQueryNo, batchNo));
        for (ClueTaskQueryLog queryLog : logList) {
             taskQueryThreadPool.execute(() -> {
                queryClue(queryLog);
            });
        }

    }

    @Override
    public void queryClue(ClueTaskQueryLog queryLog) {

        log.info("queryClue {}", queryLog);
        CarPlate carPlate = CarPlate.getInstance(queryLog.getTpCarConfigCode());
        CarPlateService carPlateService = carPlateServiceMap.get(carPlate.bean);
        carPlateService.query(queryLog);
        // 更新日志
        updateById(queryLog);


        // 更新任务邀约状态
        ClueRecordTask recordTask = clueRecordTaskService.getOne(new LambdaQueryWrapper<ClueRecordTask>().eq(ClueRecordTask::getTaskNo, queryLog.getTaskNo()));
        if (null == recordTask) {
            return;
        }
        if (CarPlate.汽车之家.code.equals(recordTask.getTpCarConfigCode())) {
            // 保证幂等性
            if (!ClueInviteStatus.已邀约.status.equals(recordTask.getInviteStatus())) {
                /*未分发 未申诉或申诉失败 表示已邀约*/
                if (QczjDistributeStatus.未分发.status.equals(queryLog.getQczjDistributeStatus()) &&
                        QczjAppealStatus.未申诉或申诉失败.status.equals(queryLog.getQczjAppealStatus())) {
                    recordTask.setInviteStatus(ClueInviteStatus.已邀约.status);
                    recordTask.setInviteTime(DateUtil.date());


                    if (StringUtils.isNotBlank(recordTask.getChannelInviteStatus())) {
                        recordTask.setChannelInviteStatus(ChannelInviteStatus.成功.status);
                        recordTask.setChannelInviteTime(recordTask.getInviteTime());
                    }

                    clueRecordTaskService.updateById(recordTask);
                }
                /*未分发 申诉成功邀约失败 表示邀约失败*/
                if (QczjDistributeStatus.未分发.status.equals(queryLog.getQczjDistributeStatus()) &&
                        QczjAppealStatus.申诉成功.status.equals(queryLog.getQczjAppealStatus())) {
                    recordTask.setInviteStatus(ClueInviteStatus.邀约失败.status);
                    recordTask.setInviteTime(DateUtil.date());


                    if (StringUtils.isNotBlank(recordTask.getChannelInviteStatus())) {
                        recordTask.setChannelInviteStatus(ChannelInviteStatus.失败.status);
                        recordTask.setChannelInviteTime(recordTask.getInviteTime());
                    }

                    clueRecordTaskService.updateById(recordTask);
                }
            }
        } else if (CarPlate.小喇叭.code.equals(recordTask.getTpCarConfigCode())) {
            // 保证幂等性
            if (!ClueInviteStatus.已邀约.status.equals(recordTask.getInviteStatus())) {
                /*已建单 表示已邀约*/
                if (XlbClueStatus.成功.status.equals(queryLog.getXlbBuildStatus())) {
                    recordTask.setInviteStatus(ClueInviteStatus.已邀约.status);
                    recordTask.setInviteTime(DateUtil.date());

                    if (StringUtils.isNotBlank(recordTask.getChannelInviteStatus())) {
                        recordTask.setChannelInviteStatus(ChannelInviteStatus.成功.status);
                        recordTask.setChannelInviteTime(recordTask.getInviteTime());
                    }
                    clueRecordTaskService.updateById(recordTask);
                }else{

                    recordTask.setInviteStatus(ClueInviteStatus.邀约失败.status);
                    recordTask.setInviteTime(DateUtil.date());
                    if (StringUtils.isNotBlank(recordTask.getChannelInviteStatus())) {
                        recordTask.setChannelInviteStatus(ChannelInviteStatus.失败.status);
                        recordTask.setChannelInviteTime(recordTask.getInviteTime());
                    }
                    clueRecordTaskService.updateById(recordTask);
                }
            }
        }


    }


}
